<template>
  <div class="login_conent">
    <div class="login_box">
      <div class="img_box">
        <img src="../../assets/logo.png" alt="" />
      </div>
      <el-form ref="loginFormRef" :rules="rules" :model="loginForm">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名">
            <i slot="prefix" class="el-input__icon el-icon-user-solid"></i
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            show-password
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
          >
            <i slot="prefix" class="el-input__icon el-icon-s-goods"></i
          ></el-input>
        </el-form-item>
        <el-form-item class="btn">
          <el-button type="primary" @click="handleSumbit">登录</el-button>
          <el-button type="info" @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { loginApi } from '../../api/index';

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
      },
    };
  },
  methods: {
    // 点击了重置
    handleReset() {
      this.$refs.loginFormRef.resetFields();
    },
    // 点击了登录
    handleSumbit() {
      this.$refs.loginFormRef.validate((valid) => {
        if (!valid) return;
        this.onLogin();
      });
    },
    // 登录接口
    async onLogin() {
      const res = await loginApi(this.loginForm);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      // 保存token
      window.sessionStorage.setItem('token', res.data.token);
      // 路由跳转
      this.$router.replace('/home');
    },
  },
};
</script>

<style lang='less' scoped>
.login_conent {
  background-color: #2b4b6b;
  height: 100%;
  .login_box {
    width: 400px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .img_box {
      width: 130px;
      height: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      background: #fff;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    .el-form {
      padding: 10px;
      margin-top: 100px;
      .btn {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
